﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2011/12/11 11:41:30
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Consts;

namespace Sector7.Framework.Utility
{
    /// <summary>
    /// XML辅助类
    /// </summary>
    public class XmlUtil
    {
        #region Public functions
        /// <summary>
        /// 打开Xml文件
        /// </summary>
        /// <param name="filePath">Xml文件地址</param>
        /// <returns>XmlDocument对象</returns>
        public static XmlDocument Open(string filePath)
        {
            XmlDocument xmlDoc = null;

            try
            {
                xmlDoc = new XmlDocument();
                xmlDoc.Load(filePath);
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.ERR, C_ModuleName.FRAMEWORK, "XmlUtil.Open: Load xml file failed.");
                Log.WriteLine(LogSeverityType.ERR, C_ModuleName.FRAMEWORK, e);

                xmlDoc = null;
            }

            return xmlDoc;
        }

        /// <summary>
        /// 获取Xml节点列表中第一个匹配的Xml节点
        /// </summary>
        /// <param name="xmlNodeList">Xml节点列表</param>
        /// <param name="nodeName">指定的节点名称</param>
        /// <returns>第一个匹配的Xml节点</returns>
        public static XmlNode GetXmlNode(XmlNodeList xmlNodeList, string nodeName)
        {
            XmlNode node = null;

            nodeName = nodeName.Trim().ToUpper();
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NodeType == XmlNodeType.Element)
                {
                    if (xmlNode.Name.Trim().ToUpper() == nodeName)
                    {
                        node = xmlNode;
                    }
                    else
                    {
                        if (xmlNode.HasChildNodes)
                        {
                            node = GetXmlNode(xmlNode.ChildNodes, nodeName);
                        }
                    }

                    if (node != null)
                    {
                        break;
                    }
                }
            }

            return node;
        }

        /// <summary>
        /// 获取Xml节点列表中匹配的所有Xml节点（递归查找）
        /// </summary>
        /// <param name="xmlNodeList">Xml节点列表</param>
        /// <param name="nodeName">指定的节点名称</param>
        /// <returns>匹配的所有Xml节点</returns>
        public static List<XmlNode> GetXmlNodeList(XmlNodeList xmlNodeList, string nodeName)
        {
            List<XmlNode> nodeList = new List<XmlNode>();

            nodeName = nodeName.Trim().ToUpper();
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NodeType == XmlNodeType.Element)
                {
                    if (xmlNode.Name.Trim().ToUpper() == nodeName)
                    {
                        nodeList.Add(xmlNode);
                    }
                    else
                    {
                        if (xmlNode.HasChildNodes)
                        {
                            nodeList.AddRange(GetXmlNodeList(xmlNode.ChildNodes, nodeName));
                        }
                    }
                }
            }

            return nodeList;
        }

        /// <summary>
        /// 获取Xml节点列表中第一层匹配的所有Xml节点（非递归查找）
        /// </summary>
        /// <param name="xmlNodeList">Xml节点列表</param>
        /// <param name="nodeName">指定的节点名称</param>
        /// <returns>匹配的所有Xml节点</returns>
        public static List<XmlNode> GetXmlNodeListWithoutChildren(XmlNodeList xmlNodeList, string nodeName)
        {
            List<XmlNode> nodeList = new List<XmlNode>();

            nodeName = nodeName.Trim().ToUpper();
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NodeType == XmlNodeType.Element)
                {
                    if (xmlNode.Name.Trim().ToUpper() == nodeName)
                    {
                        nodeList.Add(xmlNode);
                    }
                }
            }

            return nodeList;
        }

        /// <summary>
        /// 获取Xml节点属性值
        /// </summary>
        /// <param name="xmlNode">Xml节点</param>
        /// <param name="attributeName">属性名称</param>
        /// <returns>属性值</returns>
        public static string GetXmlNodeAttributeValue(XmlNode xmlNode, string attributeName)
        {
            string attributeValue = null;

            attributeName = attributeName.Trim().ToUpper();
            if (xmlNode.Attributes != null)
            {
                foreach (XmlAttribute xmlAttribute in xmlNode.Attributes)
                {
                    if (xmlAttribute.NodeType == XmlNodeType.Attribute)
                    {
                        if (xmlAttribute.Name.Trim().ToUpper() == attributeName)
                        {
                            attributeValue = xmlAttribute.Value;
                            break;
                        }
                    }
                }
            }

            return attributeValue;
        }
        #endregion
    }
}
